package com.findroader.blog.web.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.portlet.ModelAndView;

import com.findroader.blog.dto.PageQueryDTO;
import com.findroader.blog.dto.QueryParamDTO;
import com.findroader.blog.dto.ResponseResultDTO;
import com.findroader.blog.dto.ResultDTO;
import com.findroader.blog.model.ArticleBean;
import com.findroader.blog.model.ArticleCommentsBean;
import com.findroader.blog.model.ArticleTypeBean;
import com.findroader.blog.page.Page;
import com.findroader.blog.service.ArticleService;

@Controller
@RequestMapping("/frManager")
public class ManagerController {

	@Resource
	private ArticleService service;

	@RequestMapping("/index")
	public String index(){
		
		return "index";
	}
	
	@RequestMapping("/article/list/{pageNo}")
	public String articleList(@PathVariable("pageNo")int pageNo,HttpServletRequest request){
	   pageNo=	pageNo<=0?1:pageNo;
	   Page<ArticleBean> page = new Page<ArticleBean>(100);
	   page.setPageNo(pageNo);
	   
	   request.setAttribute("page", service.queryArticleList(page, null));
	   return "article/articleList";
	}
	
	@RequestMapping("/article/view")
	public String articleView(Integer articleId,HttpServletRequest request){
	  List<ArticleTypeBean >types =service.queryTypeList();	
	  request.setAttribute("types", types);
	 if(articleId!=null&&articleId!=0){
		 ArticleBean article= service.get(articleId);
		 request.setAttribute("article", article);
	 }	
	 return "article/article";	
	}
	
	
	
	@RequestMapping("/article/get/{articleId}")
	public @ResponseBody ArticleBean getArticle(@PathVariable("articleId")int id) {

		return service.get(id);
	}

	@RequestMapping("/article/update")
	public @ResponseBody ResponseResultDTO save(ArticleBean article) {
		ResponseResultDTO result = new ResponseResultDTO();

		try {
			if (article.getArticleId() != null && article.getArticleId() > 0) {
				service.update(article);
			} else {
				service.save(article);
			}
			result.setSuccess(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("系统发生异常:" + getExceptionCause(e));

		}
		return result;
	}

	@RequestMapping("/article/del/{articleId}")
	public @ResponseBody ResponseResultDTO deleteArticle(@PathVariable("articleId") int id) {
		ResponseResultDTO result = new ResponseResultDTO();

		try {
			int count = service.delete(id);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("系统发生异常:" + getExceptionCause(e));
		}
		return result;
	}

	@RequestMapping("/type/update")
	public @ResponseBody ResponseResultDTO saveType(ArticleTypeBean type) {
		ResponseResultDTO result = new ResponseResultDTO();

		try {
			if (type.getTypeId() != null && type.getTypeId() > 0) {
				service.updateType(type);
			} else {
				int count = service.saveArticleType(type);
			}
			result.setSuccess(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("系统发生异常:" + getExceptionCause(e));
		}
		return result;
	}
   @RequestMapping("/type/get/{typeKey}")
	public @ResponseBody ArticleTypeBean getType(String typeKey) {
		return service.getType(typeKey);
	}

   @RequestMapping("/type/del/{typeId}")
	public @ResponseBody ResponseResultDTO deleteType(@PathVariable("typeId") int typeId) {
		ResponseResultDTO result = new ResponseResultDTO();

		try {
			int count = service.deleteType(typeId);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("系统发生异常:" + getExceptionCause(e));
		}
		return result;
	}
   
   @RequestMapping("/comments/list")
   public String queryComments(PageQueryDTO pageParam,HttpServletRequest request){
	   int pageNo=pageParam.getCurrentPage();
	   int pageSize=pageParam.getNumPerPage();
	   Page<ArticleCommentsBean> page = new Page<ArticleCommentsBean>();
	   page.setPageNo(pageNo==0?1:pageNo);
	   page.setPageSize(pageSize==0?20:pageSize);
	   
	   QueryParamDTO param = new QueryParamDTO();
	   param.setName(pageParam.getName());
	   param.setKeyWord(pageParam.getKeyWord());
	   request.setAttribute("pageParam", pageParam);
	   page = service.queryAllComments(page, param);
	   request.setAttribute("page", page);
	   return "/comment/commentList";
   }
   
   
   @RequestMapping("/comment/del/{commentId}")
	public @ResponseBody ResultDTO deleteCommnet(@PathVariable("commentId")int commentId) {
	   ResultDTO result = new ResultDTO();

		try {
			service.deleteComment(commentId);
			result.setMessage("操作成功");
			result.setStatusCode("200");
		} catch (Exception e) {
			result.setStatusCode("500");
			result.setMessage("系统发生异常:" + getExceptionCause(e));
		}
		return result;
	}
   
   @RequestMapping("/comment/replyView")
	public  String replyView( int commentId) {
	    
        return "comment/reply";
	}
   
   
    @RequestMapping(value="/comment/reply",method=RequestMethod.POST)
	public @ResponseBody ResultDTO reply(@RequestParam(value="reply")String reply, @RequestParam(value="commentId") int commentId) {
    	ResultDTO result = new ResultDTO();

		try {
			service.reply(reply, commentId);
			result.setMessage("操作成功");
		} catch (Exception e) {
			result.setStatusCode("404");
			result.setMessage("系统发生异常:" + getExceptionCause(e));
		}
		return result;
	}

	private String getExceptionCause(Exception e) {
		Throwable throwable = e.getCause();
		String t = e.getMessage();
		while (throwable != null) {
			t = throwable.getMessage();
			throwable = throwable.getCause();
		}
		return t;
	}
}
